package com.wk.masterNode;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;

import javax.crypto.Mac;

import com.wk.database.IdToMachine;
import com.wk.database.MachinesMap;
import com.wk.job.Job;



	/***
	 * 负责消费jobsqueue中的job
	 * 1. 初始化映射表。
	 * 2. 获取目标机器。
	 * 3. 如果对方空闲发送job。否则等待。
	 * 4. 
	 * @author wkhua
	 *
	 */	
	public class Consumer implements Runnable{

		@SuppressWarnings("unused")
		private LinkedBlockingQueue<Job> jobsqueue = null;
	
		private IdToMachine idToMachine = new IdToMachine();
		private DistrubuteJob  distributejob= null;
		
		private ConcurrentHashMap<String, Job> distributedjobsMap = null;
		private boolean isWorking = true;
		public Consumer(LinkedBlockingQueue<Job> jobsqueue, ConcurrentHashMap<String, Job> distributedjobsMap){
			this.jobsqueue = jobsqueue;
			this.distributedjobsMap = distributedjobsMap;
			MachinesMap machinesMap = new MachinesMap();

			machinesMap.initFromDataBase(idToMachine);
			
			this.distributejob = new DistrubuteJob(jobsqueue, idToMachine,distributedjobsMap);
			
		}
	

		@Override
		public void run() {
			
				while(isWorking){
					System.out.println("working");
					//更新映射表
					distributejob.distributeAJob();
					Set<String> keys = distributedjobsMap.keySet();
					
					for(String key : keys){
						System.out.println(key+" "+distributedjobsMap.get(key));
					}
					try {
						Thread.sleep( 5* 1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				
				}
		}
		
		
		
	
		
}
